var bootbox_global;
var grid;

//删除
function deleteObj(objid)
{

    bootbox= bootbox_global;
     //alert(objid);

    bootbox_global.dialog({
        message: "确定要删除吗?",
        buttons: {
            Cancel: {
                label: "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                callback: function () {
                }
            }
            , OK: {
                label: "<i class='icon-ok'></i> 确定",
                "className": "btn-sm btn-success",
                callback: function () {


                    //发送请求
                    send_request_direct("/budget/deleteMaterial", "obj_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });

                }
            }
        }
    });

}


define([
    "jquery",
    "IO",
    "bootbox"
], function ($, IO, bootbox) {

    bootbox_global = bootbox;
    $(function () {


        //单位下拉框
        var columns_select ={name_dataIndex :'name',value_dataIndex :'id'};
         var  grid_select = new Select_Grid("/budget/getUnitListPaging",$("#unitselect"),columns_select,"rows=100000");

                 grid_select.Init(function(){

                 });

        //种类
        var columns_type ={name_dataIndex :'name',value_dataIndex :'id'};
         var  grid_type = new Select_Grid("/budget/GetMaterialTypeList",$("#typeselect"),columns_type,"rows=100000&select_flag=1");

        grid_type.Init(function(){



                         });

        //每列数据列性
        var columns = [

            {
                title: '编号', dataIndex: 'User_ID', dataSource: function (data, datarow, gridobj, current_column) {

                return "<span>" + parseInt((gridobj.current_page - 1) * gridobj.page_rows + parseInt(datarow) + 1) + "</span>";
            }
            },
            //{ title: '种类id', dataIndex: 'type_id',},
            {title: '名称', dataIndex: 'name',},
            {title: '类别', dataIndex: 'type_name',},
            {title: '单位', dataIndex: 'unit_name',},
            {title: '比例', dataIndex: 'ratio',},
            {title: '公式', dataIndex: 'formula',},
            {title: '备注', dataIndex: 'remark',},
            {
                title: '操作', dataIndex: 'User_ID', dataSource: function (data, datarow, gridobj, current_column) {

                var html = "<div class='div_btn'> <button onclick=\"deleteObj(" + data[datarow].id + ")\"  class='btn btn-danger'>删除</button>";


                html = html + "</div>"

                return html;
            }
            },


        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/budget/getMaterialListPaging", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param = "name=" + $("#name").val();

            return param;

        }

        dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });

        //新增
        $("#btn_add").bind("click", function () {


            var alertarray = {
                form: {api: "/budget/addMaterial", name: "alertform", id: "alertform"},
                inputs: [

                    {title: "名称", type: "text", placeholder: "", verify: "empty", name: "name", id: "name"},
                    {title: "种类", type: "select", id: "type_id",form_name:"type_id",copyid:"typeselect"},
                    {title: "单位", type: "select", id: "unit_id",form_name:"unit_id",copyid:"unitselect"},

                    {title: "公式", type: "text", placeholder: "", verify: "empty", name: "formula", id: "formula"},
                    {title: "比例", type: "text", placeholder: "", verify: "empty", name: "ratio", id: "ratio"},


                    {title: "备注", type: "text", placeholder: "", verify: "", name: "remark", id: "remark"},
                    //{title: "图片", type: "img", placeholder: "", verify: "empty", name: "pic_url2", id: "upimgs2"},


                ]

            }
            bootbox.dialog({
                title: "新增",
                message: generateAlertForm(alertarray),
                buttons: {
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-danger",
                        "callback": function () {
                        }
                    },
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-success",
                        "callback": function () {

                            //验证
                            if (check_form_info_all(alertarray.form.name) == false)
                                return false;

                            //发送请求
                            ajax_request(alertarray.form.name, "&parent_id=-1", function process_data(data) {


                                //操作成功
                                AlertSuccess(bootbox, "操作成功", function () {

                                    grid.Init();


                                });


                            }, function process_fail(data) {

                                //操作失败
                                AlertError(bootbox, data.msg);


                            });


                        }
                    }
                }
            });


        });

    });


});